using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class mycontrol : System.Web.UI.Page
{
    public static  string prefixLink;
    public static  string suffix;
    public static  string prefixDirecotry;
    public static  string text_currentDirectory;
    public static  string currentLink;


    private void dataSourceBind()
    {
        if (!prefixDirecotry.EndsWith("/"))
        {
            prefixDirecotry += "/";
        }

        if (!prefixLink.EndsWith("/"))
        {
            prefixLink += "/";
        }

        currentLink = prefixLink + suffix;
        text_currentDirectory = prefixDirecotry + suffix;
        currentDirectory.Text = text_currentDirectory;
        if (text_currentDirectory == "")
            return;
        DirectoryInfo dirInfo = new DirectoryInfo(text_currentDirectory);
        DataTable dt_Files = new DataTable();      

        FileInfo[] fileInfo = dirInfo.GetFiles();

        dt_Files.Columns.Add("name");
        dt_Files.Columns.Add("currentLink");

        for (int k = 0; k < fileInfo.Length; k++)
        {
            dt_Files.Rows.Add(fileInfo[k].Name,currentLink + fileInfo[k].Name);           
        }

        Label2.Text = currentLink;
        GridView1.DataSource = dt_Files;
        directory.DataSource = dirInfo.GetDirectories();
        GridView1.DataBind();
        directory.DataBind();      
        

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ( IsPostBack )
        {        

        }
        else
        {
            prefixDirecotry = MapPath("");
            prefixLink = Request.Url.AbsoluteUri;
            prefixLink = prefixLink.Replace(Request.Url.Segments[Request.Url.Segments.Length - 1], "");
            suffix = "";          

            dataSourceBind();
        }
        
    }

    protected void upload_Click(object sender, EventArgs e)
    {
        string AppPath = Request.PhysicalApplicationPath;
        if (FileUpload1.HasFile)
        {
            string savePath = text_currentDirectory + FileUpload1.FileName;
            FileUpload1.SaveAs(savePath);
        }
        else
        {
            Response.Write("<script language='javascript'>window.alert('failed')</script>");
        }

    }

  

    protected void LinkButton1_Command1(object sender, CommandEventArgs e)
    {
        File.Delete(MapPath("") + "/" + e.CommandArgument.ToString());
    }

    protected void LinkButton2_Command(object sender, CommandEventArgs e)
    {
        suffix = suffix + e.CommandArgument + "/";
        suffix = suffix.TrimStart('/');        
        dataSourceBind();

    }

    protected void saveButton_Click(object sender, EventArgs e)
    {
        if (nameBox.Text != string.Empty)
        {
            string FilePathSave = text_currentDirectory + nameBox.Text;
            File.WriteAllText(FilePathSave, contentBox.Text);
        }
        else
        {
            Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>");
        }
        nameBox.Text="";
        contentBox.Text="";
    }


    protected void browseDirec_Click(object sender, EventArgs e)
    {
        suffix = text_currentDirectory.Replace(prefixDirecotry, "");
        text_currentDirectory = currentDirectory.Text;
        dataSourceBind();
    }

    protected void parentDirectory_Click(object sender, EventArgs e)
    {
        if (suffix != "" )
        {            
            suffix = suffix.Trim('/');
            int n = suffix.LastIndexOf('/');
            suffix = suffix.Substring(0, n + 1);
        }
               
        dataSourceBind();
    }
}
